package com.example.springboot.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.example.springboot.controller.request.BasePageRequest;
import com.example.springboot.entity.Bike;
import com.example.springboot.entity.Category;
import com.example.springboot.mapper.BikeMapper;
import com.example.springboot.service.BikeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * ClassName:BikeServiceImpl
 * Package:com.example.springboot.service.impl
 * Description:
 *
 * @Author: Gao
 * @Create: 2023/2/6 - 19:17
 */
@Service
public class BikeServiceImpl implements BikeService {
    @Autowired
    BikeMapper bikeMapper;


    @Override
    public List<Bike> list() {

        return bikeMapper.list();
    }

    @Override
    public Object page(BasePageRequest basePageRequest) {
        PageHelper.startPage(basePageRequest.getPageNum(), basePageRequest.getPageSize());
        List<Bike> bikes = bikeMapper.listByCondition(basePageRequest);
        PageInfo<Bike> bikePageInfo = new PageInfo<>(bikes);
        return bikePageInfo;
    }

    @Override
    public void save(Bike bike) {
        bike.setCategory(category(bike.getCategories()));
        bikeMapper.save(bike);
    }

    @Override
    public void deleteById(int id) {
        bikeMapper.deleteById(id);
    }

    @Override
    public void update(Bike bike) {
        bike.setCategory(category(bike.getCategories()));
        bikeMapper.updateById(bike);
    }

    @Override
    public Bike getById(int id) {
        return bikeMapper.getById(id);
    }

    @Override
    public Bike getByName(String name) {
        return bikeMapper.getByName(name);
    }


    private String category(List<String> categories) {
        StringBuilder sb = new StringBuilder();
        if (CollUtil.isNotEmpty(categories)){
            categories.forEach(v -> sb.append(v).append(">"));
            return sb.substring(0,sb.length()-1);
        }
        return sb.toString();
    }

}
